﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using HHG.DBUtility;//Please add references
namespace OnLineExam.DAL
{
	/// <summary>
	/// 数据访问类:px_Course
	/// </summary>
    public partial class px_Course
    {
        public px_Course()
        { }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ID", "px_Course");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from px_Course");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(OnLineExam.Model.px_Course model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into px_Course(");
            strSql.Append("c_title,DepID,ClassID,c_content,c_type,c_path,c_startDate,c_endDate,alowUserJobType,alowUserID,PaperID,completeUserID,completeDate,c_addtime)");
            strSql.Append(" values (");
            strSql.Append("@c_title,@DepID,@ClassID,@c_content,@c_type,@c_path,@c_startDate,@c_endDate,@alowUserJobType,@alowUserID,@PaperID,@completeUserID,@completeDate,@c_addtime)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@c_title", SqlDbType.NVarChar,100),
					new SqlParameter("@DepID", SqlDbType.Int,4),
					new SqlParameter("@ClassID", SqlDbType.Int,4),
					new SqlParameter("@c_content", SqlDbType.NVarChar,4000),
					new SqlParameter("@c_type", SqlDbType.NVarChar,50),
					new SqlParameter("@c_path", SqlDbType.NVarChar,500),
					new SqlParameter("@c_startDate", SqlDbType.DateTime),
					new SqlParameter("@c_endDate", SqlDbType.DateTime),
					new SqlParameter("@alowUserJobType", SqlDbType.VarChar,50),
					new SqlParameter("@alowUserID", SqlDbType.NVarChar,50),
					new SqlParameter("@PaperID", SqlDbType.Int,4),
					new SqlParameter("@completeUserID", SqlDbType.NVarChar,50),
					new SqlParameter("@completeDate", SqlDbType.DateTime),
					new SqlParameter("@c_addtime", SqlDbType.DateTime)};
            parameters[0].Value = model.c_title;
            parameters[1].Value = model.DepID;
            parameters[2].Value = model.ClassID;
            parameters[3].Value = model.c_content;
            parameters[4].Value = model.c_type;
            parameters[5].Value = model.c_path;
            parameters[6].Value = model.c_startDate;
            parameters[7].Value = model.c_endDate;
            parameters[8].Value = model.alowUserJobType;
            parameters[9].Value = model.alowUserID;
            parameters[10].Value = model.PaperID;
            parameters[11].Value = model.completeUserID;
            parameters[12].Value = model.completeDate;
            parameters[13].Value = model.c_addtime;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(OnLineExam.Model.px_Course model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update px_Course set ");
            strSql.Append("c_title=@c_title,");
            strSql.Append("DepID=@DepID,");
            strSql.Append("ClassID=@ClassID,");
            strSql.Append("c_content=@c_content,");
            strSql.Append("c_type=@c_type,");
            strSql.Append("c_path=@c_path,");
            strSql.Append("c_startDate=@c_startDate,");
            strSql.Append("c_endDate=@c_endDate,");
            strSql.Append("alowUserJobType=@alowUserJobType,");
            strSql.Append("alowUserID=@alowUserID,");
            strSql.Append("PaperID=@PaperID,");
            strSql.Append("completeUserID=@completeUserID,");
            strSql.Append("completeDate=@completeDate,");
            strSql.Append("c_addtime=@c_addtime");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@c_title", SqlDbType.NVarChar,100),
					new SqlParameter("@DepID", SqlDbType.Int,4),
					new SqlParameter("@ClassID", SqlDbType.Int,4),
					new SqlParameter("@c_content", SqlDbType.NVarChar,4000),
					new SqlParameter("@c_type", SqlDbType.NVarChar,50),
					new SqlParameter("@c_path", SqlDbType.NVarChar,500),
					new SqlParameter("@c_startDate", SqlDbType.DateTime),
					new SqlParameter("@c_endDate", SqlDbType.DateTime),
					new SqlParameter("@alowUserJobType", SqlDbType.VarChar,50),
					new SqlParameter("@alowUserID", SqlDbType.NVarChar,50),
					new SqlParameter("@PaperID", SqlDbType.Int,4),
					new SqlParameter("@completeUserID", SqlDbType.NVarChar,50),
					new SqlParameter("@completeDate", SqlDbType.DateTime),
					new SqlParameter("@c_addtime", SqlDbType.DateTime),
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = model.c_title;
            parameters[1].Value = model.DepID;
            parameters[2].Value = model.ClassID;
            parameters[3].Value = model.c_content;
            parameters[4].Value = model.c_type;
            parameters[5].Value = model.c_path;
            parameters[6].Value = model.c_startDate;
            parameters[7].Value = model.c_endDate;
            parameters[8].Value = model.alowUserJobType;
            parameters[9].Value = model.alowUserID;
            parameters[10].Value = model.PaperID;
            parameters[11].Value = model.completeUserID;
            parameters[12].Value = model.completeDate;
            parameters[13].Value = model.c_addtime;
            parameters[14].Value = model.ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from px_Course ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
            parameters[0].Value = ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from px_Course ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public OnLineExam.Model.px_Course GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,c_title,DepID,ClassID,c_content,c_type,c_path,c_startDate,c_endDate,alowUserJobType,alowUserID,PaperID,completeUserID,completeDate,c_addtime from px_Course ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
            parameters[0].Value = ID;

            OnLineExam.Model.px_Course model = new OnLineExam.Model.px_Course();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["c_title"] != null && ds.Tables[0].Rows[0]["c_title"].ToString() != "")
                {
                    model.c_title = ds.Tables[0].Rows[0]["c_title"].ToString();
                }
                if (ds.Tables[0].Rows[0]["DepID"] != null && ds.Tables[0].Rows[0]["DepID"].ToString() != "")
                {
                    model.DepID = int.Parse(ds.Tables[0].Rows[0]["DepID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ClassID"] != null && ds.Tables[0].Rows[0]["ClassID"].ToString() != "")
                {
                    model.ClassID = int.Parse(ds.Tables[0].Rows[0]["ClassID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["c_content"] != null && ds.Tables[0].Rows[0]["c_content"].ToString() != "")
                {
                    model.c_content = ds.Tables[0].Rows[0]["c_content"].ToString();
                }
                if (ds.Tables[0].Rows[0]["c_type"] != null && ds.Tables[0].Rows[0]["c_type"].ToString() != "")
                {
                    model.c_type = ds.Tables[0].Rows[0]["c_type"].ToString();
                }
                if (ds.Tables[0].Rows[0]["c_path"] != null && ds.Tables[0].Rows[0]["c_path"].ToString() != "")
                {
                    model.c_path = ds.Tables[0].Rows[0]["c_path"].ToString();
                }
                if (ds.Tables[0].Rows[0]["c_startDate"] != null && ds.Tables[0].Rows[0]["c_startDate"].ToString() != "")
                {
                    model.c_startDate = DateTime.Parse(ds.Tables[0].Rows[0]["c_startDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["c_endDate"] != null && ds.Tables[0].Rows[0]["c_endDate"].ToString() != "")
                {
                    model.c_endDate = DateTime.Parse(ds.Tables[0].Rows[0]["c_endDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["alowUserJobType"] != null && ds.Tables[0].Rows[0]["alowUserJobType"].ToString() != "")
                {
                    model.alowUserJobType = ds.Tables[0].Rows[0]["alowUserJobType"].ToString();
                }
                if (ds.Tables[0].Rows[0]["alowUserID"] != null && ds.Tables[0].Rows[0]["alowUserID"].ToString() != "")
                {
                    model.alowUserID = ds.Tables[0].Rows[0]["alowUserID"].ToString();
                }
                if (ds.Tables[0].Rows[0]["PaperID"] != null && ds.Tables[0].Rows[0]["PaperID"].ToString() != "")
                {
                    model.PaperID = int.Parse(ds.Tables[0].Rows[0]["PaperID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["completeUserID"] != null && ds.Tables[0].Rows[0]["completeUserID"].ToString() != "")
                {
                    model.completeUserID = ds.Tables[0].Rows[0]["completeUserID"].ToString();
                }
                if (ds.Tables[0].Rows[0]["completeDate"] != null && ds.Tables[0].Rows[0]["completeDate"].ToString() != "")
                {
                    model.completeDate = DateTime.Parse(ds.Tables[0].Rows[0]["completeDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["c_addtime"] != null && ds.Tables[0].Rows[0]["c_addtime"].ToString() != "")
                {
                    model.c_addtime = DateTime.Parse(ds.Tables[0].Rows[0]["c_addtime"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,c_title,DepID,ClassID,c_content,c_type,c_path,c_startDate,c_endDate,alowUserJobType,alowUserID,PaperID,completeUserID,completeDate,c_addtime ");
            strSql.Append(" FROM px_Course ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,c_title,DepID,ClassID,c_content,c_type,c_path,c_startDate,c_endDate,alowUserJobType,alowUserID,PaperID,completeUserID,completeDate,c_addtime ");
            strSql.Append(" FROM px_Course ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM px_Course ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ID desc");
            }
            strSql.Append(")AS Row, T.*  from px_Course T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "px_Course";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

